package site.hanzhe.did.entity;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Setter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Docker镜像实体类
 *
 * @author 张涵哲
 * @since 2024-06-14 19:19:26
 */
@Setter
public class DockerImage {

    private JSONObject jsonObject;

    /**
     * 将JSON字符串解析为DockerImage对象
     *
     * @author 张涵哲
     * @since 2024-06-14 20:30:05
     */
    public static DockerImage parse(String responseBody) {
        DockerImage dockerImage = new DockerImage();
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        dockerImage.setJsonObject(jsonObject);
        return dockerImage;
    }

    /**
     * 获取Digest
     *
     * @author 张涵哲
     * @since 2024-06-15 00:25:37
     */
    public String getDigest() {
        return jsonObject.getByPath("config.digest", String.class);
    }

    /**
     * 获取分层列表
     *
     * @author 张涵哲
     * @since 2024-06-15 00:23:08
     */
    public List<Layer> getLayers() {
        JSONArray layers = jsonObject.getJSONArray("layers");
        return layers.stream().map(item -> {
            JSON parse = JSONUtil.parse(item);
            return parse.toBean(Layer.class);
        }).collect(Collectors.toList());
    }

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this.jsonObject);
    }

}
